//
//  WelcomeViewController.swift
//  sinaWeibo
//
//  Created by liii on 16/7/18.
//  Copyright © 2016年 liii. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        // 自动设置大小
        self.view = backgroundImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    /// 搭建UI并布局子控件
    private func setupUI() {
        // 添加子控件
        self.view.addSubview(iconImageView)
        self.view.addSubview(welcomeLabel)
        
        // 布局子控件
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-300)
            
            // 设置尺寸
            make.width.equalTo(100)
            make.height.equalTo(100)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(20)
        }
        
        // 设置头像
        iconImageView.sd_setImageWithURL(sharedUser.headImageURL)
        // 设置圆角
        iconImageView.layer.cornerRadius = 50
        iconImageView.layer.masksToBounds = true
        // 设置边框
        iconImageView.layer.borderWidth = 1
        iconImageView.layer.borderColor = UIColor.orangeColor().CGColor
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    /// 让头像执行动画
    private func startAnimation() {
        /**
        usingSpringWithDamping: 弹力系数(取值:0~1,值越小越弹)
        initialSpringVelocity:  加速度(一般是弹力系数的10倍)
        options:                动画效果(OC中使用位枚举,swift使用数组)
        */
        let offsetY = -UIScreen.mainScreen().bounds.height + 300
        // 更新约束
        self.iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(offsetY)
        }
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9, options: [], animations: { () -> Void in
        
            // 强制刷新页面(这段代码必须在动画闭包中执行)
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 将隐藏的标签显示出来
                UIView.animateWithDuration(0.3, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                })
                
                // gcd延迟执行
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { () -> Void in

                    // 发起通知, 切换控制器(object: 代表参数一般用于区别不同的发起者, 多参可以使用userInfo)
                    defaultCenter.postNotificationName(switchRootVcNotification, object: "welcomeVC")
                })
        }
    }
    
    /// 懒加载子控件
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))

    private lazy var welcomeLabel: UILabel = UIFactory.labelWithText(((UserAccount.sharedAccount.name ?? "") + "  欢迎归来"), fontSize: 16, textColor: UIColor.grayColor(), alpha: 0)
}
